package com.jf.springboot.utils;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-12-06
 * Time:11:14
 */
public class FileUtil {
    public static void saveFile(MultipartFile file, String folderName) throws IOException {
        String classPath  = ResourceUtils.getURL("classpath:").getPath();
        String folderPath = classPath + folderName;
        File folder = new File(folderPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String fileName = file.getOriginalFilename();
        file.transferTo(new File(folder.getAbsolutePath() + "/" + fileName));
    }
}
